Cf.DomHelper = {
	create: function(o){
		o = o || {};
		
		Cf.putIf(o, {
			tag: 'div'
			,id: Cf.prefix + '-gen' + Cf.seq()
		});
		
		var dom = document.createElement(o.tag);
		dom.id = o.id;
		if(o.cls) dom.className = Cf.isString(o.cls)? o.cls : o.cls.join(' ');
		if(o.style) Cf.DomHelper.setStyle(dom, o.style);
		if(o.html) dom.innerHTML = o.html;
		if(o.children) Cf.DomHelper.append(dom, o.children);
		
		delete o.id; delete o.tag; delete o.cls; delete o.style; delete o.html; delete o.children;
		
		for(var i in o){
			dom.setAttribute(i, o[i]);	
		}
		
		return dom;
	}
	
	,setStyle: function(dom, style){
		if(Cf.isString(style)){
			dom.setAttribute('style', style);
			return dom;
		}
		
		for(var i in style){
			Cf.isIE? dom.style.i : dom.style[i] = style[i];	
		}
		
		return dom;
	}
	
	,append: function(dom, children){
		if(!Cf.isArray(children)) children = [children];
		
		Cf.each(children, function(c){
			dom.appendChild(Cf.getDom(c));	
		});
		
		return dom;
	}
	
	,appendTo: function(children, dom){
		return Cf.DomHelper.append(dom, children);
	}
	
	,insertBefore: function(dom, newDom, ref){
		return dom.insertBefore(Cf.getDom(newDom), ref);	
	}
	
	,insertAfter: function(dom, newDom, ref){
		return Cf.DomHelper.insertBefore(dom, newDom, ref? ref.nextSibling : null);	
	}
	
	,insertAt: function(index, dom, newDom){
		index = index || 0;
		return Cf.DomHelper.insertBefore(dom, newDom, dom.children.item(index));
	}
	
	,update: function(dom, o){
		if(Cf.isString(o)){
			dom.innerHTML = o;
			return dom;	
		}
		
		dom.innerHTML = '';
		dom.appendChild(o);
		
		return dom;
	}
	
	,replace: function(dom, rep){
		
		Cf.DomHelper.insertBefore(rep.parentNode, dom, rep);
		Cf.DomHelper.remove(rep);
		
		return dom;
	}
	
	,remove: function(dom){
		dom.parentNode.removeChild(dom);	
	}
	
	,wrap: function(dom, wrap){
		wrap = wrap? Cf.getDom(wrap) : Cf.DomHelper.create();
		Cf.DomHelper.insertBefore(dom.parentNode, dom, wrap);
		wrap.appendChild(dom);
		
		return wrap;
	}
	
	,unwrap: function(dom){
		var wrap = dom.parentNode;
		Cf.DomHelper.insertBefore(wrap.parentNode, dom, wrap);
		Cf.DomHelper.remove(wrap);
		
		return dom;
	}
	
	,getClass: function(dom, returnString){
		var c = dom.className.trimAll();
		return returnString? c : c.split(' ');
	}
	
	,addClass: function(dom, c){
		var old = dom.className.trimAll();
		var regex = new RegExp(old.replace(/ /g, '|'), 'g');
		
		if(Cf.isArray(c)) c = c.join(' ');
		c.replace(regex, '');
		
		dom.className = c.trimAll();
		return dom;
	}
	
	,removeClass: function(dom, c){
		if(Cf.isArray(c)) c = c.join(' ');
		var regex = new RegExp(c.trimAll().replace(/ /g, '|'), 'g');
		dom.className = dom.className.replace(regex, '').trimAll();
		
		return dom;
	}
	
	,clear: function(dom){
		dom = dom || document;
		var childNodes = dom.childNodes;
		
		for(var i = 0; i < childNodes.length; ++i){
			item = childNodes.item(i);
			if(3 == item.nodeType){
				Cf.DomHelper.remove(item);
				--i;
			}
		}
		
		return dom;
	}
};